package com.xtecher.app.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.xtecher.app.column.bean.SplashResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 闪屏工具类
 * Created by Luhao on 2017/2/26.
 */

public class SplashUtils {

    private static final String KEY_ID = "splash_id";
    private static final String KEY_IMG = "splash_img";
    private static final String KEY_DATE = "splash_date";
    private static final String KEY_DURATION = "splash_dur";
    private static final String KEY_GUO = "splash_guo";

    // 将字符串转为时间戳    3016-11-10 20:00:00
    public static long getTime(String user_time) {
        String re_time = null;
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d;
        long l = 0;
        try {
            d = sdf.parse(user_time);
            l = d.getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return l;
    }

    /**
     * 比对时间是否应该显示
     *
     * @param ctime 当前时间时间戳
     * @return
     */
    public static boolean isBefore(Context context, long ctime) {
        String string = PreferenceUtil.getString(context, KEY_DATE);
        long time = getTime(string);
        return !TextUtils.isEmpty(string) && ctime >= time && time != 0;
    }

    /**
     * 预加载图片 并且保存到sp
     *
     * @param context
     * @param response
     */
    public static void loadImg(Context context, SplashResponse response) {
        long aLong = PreferenceUtil.getLong(context, KEY_ID);
        if (Long.parseLong(response.getId()) <= aLong) {
            return;
        }
        if (TextUtils.isEmpty(response.getBegin_date())) {
            return;
        }
        Glide.with(context)
                .load(response.getIphone6_img())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        PreferenceUtil.putLong(context, KEY_ID, Long.parseLong(response.getId()));
                        PreferenceUtil.putString(context, KEY_IMG, response.getIphone6_img());
                        PreferenceUtil.putString(context, KEY_DATE, response.getBegin_date());
                        PreferenceUtil.putInt(context, KEY_DURATION, response.getDuration());
                        PreferenceUtil.putBoolean(context, KEY_GUO, response.getPass() == 1);
                    }
                });
    }

    /**
     * 取消闪屏显示
     *
     * @param context
     */
    public static void clearSplash(Context context) {
        PreferenceUtil.putLong(context, KEY_ID, 0);
        PreferenceUtil.putString(context, KEY_IMG, "");
        PreferenceUtil.putString(context, KEY_DATE, "");
        PreferenceUtil.putString(context, KEY_DURATION, "");
    }

    /**
     * 获取缓存的图片链接
     *
     * @param context
     * @return
     */
    public static String getImage(Context context) {
        return PreferenceUtil.getString(context, KEY_IMG);
    }

    /**
     * 获取持续时间
     *
     * @param context
     * @return
     */
    public static int getDuration(Context context) {
        return PreferenceUtil.getInt(context, KEY_DURATION);
    }

    /**
     * 是否可跳过
     *
     * @param c
     * @return
     */
    public static boolean isGuo(Context c) {
        return PreferenceUtil.getBoolean(c, KEY_GUO, false);
    }
}
